import * as path from 'path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import jotaiDebugLabel from 'jotai/babel/plugin-debug-label'
import jotaiReactRefresh from 'jotai/babel/plugin-react-refresh'
import dotenv from 'dotenv';

dotenv.config();

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    global: 'window' // Maps `global` to `window`, which is the global object in browsers
  },
  plugins: [react({ babel: { plugins: [jotaiDebugLabel, jotaiReactRefresh]}})],
  server: {
    host: "127.0.0.1",
    port: 5173,
    proxy: {
      '/api': {
        // target: 'https://3665-240e-36f-994-1bc0-c470-8f61-32fe-5a03.ngrok-free.app',
        target: `https://seysoai.com/api2`,
        // target: `http://awakeai.tpddns.cn:45001`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/ws': {
        // target: 'https://3665-240e-36f-994-1bc0-c470-8f61-32fe-5a03.ngrok-free.app',
        target: `https://seysoai.com/api/ws`,
        // target: `http://awakeai.tpddns.cn:45001`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ws/, '')
      },
      '/gif': {
        // target: 'https://3665-240e-36f-994-1bc0-c470-8f61-32fe-5a03.ngrok-free.app',
        target: `https://seysoai.com/api`,
        // target: `http://192.168.196.201:5930`,// target: `http://awakeai.tpddns.cn:45001`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/gif/, '')
      },
      '/mq': {
        // target: 'https://3665-240e-36f-994-1bc0-c470-8f61-32fe-5a03.ngrok-free.app',
        target: `https://seysoai.com/api2/biz`,
        // target: `http://192.168.196.201:5930`,
        // target: `http://awakeai.tpddns.cn:45001`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mq/, '')
      }
    },
    changeOrigin: true,
    cors: {
      origin: ['http://awakeai.tpddns.cn:45930', 'http://192.168.196.201:5930/', 'http://192.168.196.248:5930/'],
      methods: ['GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
      allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Access-Control-Allow-Origin', 'Access-Control-Allow-Headers', 'Access-Control-Allow-Methods', 'Access-Control-Allow-Credentials']
    }
  },
  resolve: {
    alias: [
      {
        find: /^~.+/,
        replacement: val => {
          return val.replace(/^~/, '')
        }
      },
      {find: '@', replacement: path.resolve(__dirname, 'src')},
      {find: 'src', replacement: path.resolve(__dirname, 'src')},
      {find: '@store', replacement: path.resolve(__dirname, 'src/redux')},
      {find: '@configs', replacement: path.resolve(__dirname, 'src/configs')},
      {find: 'url', replacement: 'rollup-plugin-node-polyfills/polyfills/url'},
      {find: '@styles', replacement: path.resolve(__dirname, 'src/@core/scss')},
      {find: 'util', replacement: 'rollup-plugin-node-polyfills/polyfills/util'},
      {find: 'zlib', replacement: 'rollup-plugin-node-polyfills/polyfills/zlib'},
      {find: '@utils', replacement: path.resolve(__dirname, 'src/utility/Utils')},
      {find: '@hooks', replacement: path.resolve(__dirname, 'src/utility/hooks')},
      {find: '@assets', replacement: path.resolve(__dirname, 'src/@core/assets')},
      {find: '@layouts', replacement: path.resolve(__dirname, 'src/@core/layouts')},
      {find: 'assert', replacement: 'rollup-plugin-node-polyfills/polyfills/assert'},
      {find: 'buffer', replacement: 'rollup-plugin-node-polyfills/polyfills/buffer-es6'},
      {find: 'process', replacement: 'rollup-plugin-node-polyfills/polyfills/process-es6'},
      {find: '@components', replacement: path.resolve(__dirname, 'src/@core/components')}
    ]
  }
})
